<?php
/**
 * @修改：Chris
 * @时间:    2018-05-03 10:35:50
 * @功能: 商品管理
 */
header('Access-Control-Allow-Origin: *');
require( "config.php");

$p = $_POST;
$op = $p['op'];

// insert
if($op == 'add'){
	/*如果商品存在就修改*/
unset($data['op']);
 $data['name'] =  $p['name'];//商品名称
 $data['number'] = $p['number'];//商品编号
 $data['spec'] = $p['spec'];//型号
 $data['unitName'] = $p['unitName'];//单位
 $data['status'] = 1;//商品状态 上架
 $data['lowQty'] = $p['lowQty'];//最低库存
 $data['highQty'] = $p['highQty'];//最高库存
 $data['purPrice'] = $p['salePrice'];//进货价
 $data['salePrice'] = $p['salePrice'];//零售价
 $data['barCode'] = $p['barCode'];//条形码
 $data['companyId'] = $p['companyId'];//公司商户ID
 $id = $db->insert('ci_goods',$data);

 $info['billNo'] = '期初数量';
 $info['amount'] = $p['lowQty']*$p['salePrice'];//库存数量
 $info['billDate'] = date('Y-m-d');
 $info['invId'] = $id;
 $info['price'] = $p['salePrice'];//成本价、进货价
 $info['qty'] = $p['lowQty'];
 $info['entryId'] = 1;
 $info['transTypeName'] = '期初数量';
 $info['billType'] = 'INI';
 $id2 = $db->insert('ci_invoice_info',$info);

 $img['url'] = 'http://127.0.0.1:100/attachment/'.$p['url'];
 $img['invId'] = $id;
 $id3 = $db->insert('ci_goods_img',$img); 
 // $data['locationId'] = 1;//仓库ID
 // $data['categoryId'] = $p['categoryId'];
 // $data['barCode'] = $p['barCode'];
 // $data['locationId'] = $p['locationId'];
 // $data['locationName'] = $p['locationName'];
 // $data['pinYin'] = $p['pinYin'];
 // $data['categoryName'] = $p['categoryName'];
 // $data['salePrice'] = $p['salePrice'];//销售价
 // $data['vipPrice'] = $p['vipPrice'];//vip价
 // $data['wholesalePrice'] = $p['wholesalePrice'];//批发价

 if($id && $id2 && $id3){
	$re['status'] = '200';
	$re['msg'] = $id;
	die(json_encode($id));
 }else{
	$re['status'] = '110';
	$re['msg'] = '添加失败！';
	die(json_encode('添加失败'));
 }
 die(json_encode($re));
 }

// update
if($op == 'update'){
// unset($p['op']);
// unset($p['id']);
// unset($p['url']);
//  // $data['name'] =  $p['name'];//商品名称
//  // $data['number'] = $p['number'];//商品编号
//  // $data['status'] = 1;//商品状态 上架
//  // $data['lowQty'] = $p['lowQty'];//库存数量
//  // $data['highQty'] = $p['highQty'];//最高库存
//  // $data['purPrice'] = $p['salePrice'];//进货价
//  // $data['salePrice'] = $p['salePrice'];//零售价
//  // $data['barCode'] = $p['barCode'];//条形码
 // $where = 'id = '.$_POST['id'];
 // $id = $db->update('ci_goods',$p,$where);
	/*---------------------------*/
	 $data['name'] =  $p['name'];//商品名称
	 $data['number'] = $p['number'];//商品编号
	 $data['spec'] = $p['spec'];//型号
	 $data['unitName'] = $p['unitName'];//单位
	 $data['status'] = 1;//商品状态 上架
	 $data['lowQty'] = $p['lowQty'];//最低库存
	 $data['highQty'] = $p['highQty'];//最高库存
	 $data['purPrice'] = $p['salePrice'];//进货价
	 $data['salePrice'] = $p['salePrice'];//零售价
	 $data['barCode'] = $p['barCode'];//条形码
	 $where = 'id = '.$_POST['id'];
	 $id = $db->update('ci_goods',$data,$where);
	/*------------------------------*/


 $info['billNo'] = '期初数量';
 $info['amount'] = $p['lowQty']*$p['salePrice'];//库存数量
 $info['billDate'] = date('Y-m-d');
 $info['invId'] = $_POST['id'];
 $info['price'] = $p['salePrice'];//成本价、进货价
 $info['qty'] = $p['lowQty'];
 $info['entryId'] = 1;
 $info['transTypeName'] = '期初数量';
 $info['billType'] = 'INI';
 $where2 = 'billType = "INI" and invId = '.$_POST['id'];
 $id2 = $db->update('ci_invoice_info',$info,$where2);

 $img['url'] = 'http://127.0.0.1:100/attachment/'.$_POST['url'];
 $id3 = $db->update('ci_goods_img',$img,$where);

 if($id){
	$re['status'] = '200';
	$re['msg'] =  $op.' success!';
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }

//delete
 if($op == 'delete'){

 $id =  $p['id'];
 $where = 'id = '.$id;
 $delete['isDelete'] = 1;//假删除商品
 $id = $db->update('ci_goods',$delete,$where);
 // $id = $db->delete('ci_goods',$where);

 if($id){
	$re['status'] = '200';
	$re['msg'] =  $op.' success!';
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }

//find
 if($op == 'find'){

 $id =  $p['id'];
 $where = 'id = '.$id;
  $db->find('ci_goods','*',$where);
  $re1 = $db->fetch_assoc();

 if($re1){
	$re['status'] = '200';
	$re['msg'] =  $re1;
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }

//select
 if($op == 'select'){

 $where =  $p['where'];
 $re1 = $db->select('ci_goods','*',$where);

 if($re1){
	$re['status'] = '200';
	$re['msg'] =  $re1;
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }

 die('no op!');
